<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\User;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;

class ApplicationController extends Controller
{
    /**
     * 申请作者页面1，验证用户信息
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(){
        if(Input::get('u')){
            $time=time();
            session()->put(['vvv'=>md5('jg*&baagee'.$time.'@!#^%(*%&'.Input::get('u'))]);
            return ['c'=>1,'d'=>$time];
        }
        if(Input::get('username') && Input::get('password')){
            $up=Input::except('_token');
            if(!Session::get('vvv')){
                return ['status'=>0,'msg'=>'您未验证'];
            }
            if($userinfo=User::where($up)->first()){
                session()->put(['application_userinfo'=>$userinfo]);
                return ['status'=>1,'msg'=>'身份验证成功'];
            }else{
                return ['status'=>0,'msg'=>'用户名或者密码错误'];
            }
        }
        session(['vvv'=>'','userinfo'=>'']);
        return view('home.application.index');
    }

    /**
     * 申请作者2选择角色
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function index1(){
        if(!Session::get('application_userinfo')){
            return redirect(url('application'));
        }
        if($author=Input::get('author')){
            if($author=='000'){
                return ['status'=>0,'msg'=>'您未选择'];
            }
            session()->put(['role'=>$author]);
            return ['status'=>1,'msg'=>'进行下一步'];
        }
        session(['role'=>'']);
        return view('home.application.index1');
    }

    /**
     * 申请页面2完善信息
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function index2(){
        if(!Session::get('application_userinfo') && !Session::get('role')){
            return redirect(url('application'));
        }
        if($input=Input::except('_token')){
            $id=Session::get('application_userinfo')['id'];
            if(!isset($input['age']) || !isset($input['work']) || !isset($input['sex']) || !isset($input['education']) || !isset($input['address'])){
                return ['status'=>0,'msg'=>'信息不完整'];
            }
            $input['role']=Session::get('role');
            $input['is_author']=2;
            if(User::where('id',$id)->update($input)){
                return ['status'=>1,'msg'=>'信息录入成功'];
            }else{
                return ['status'=>0,'msg'=>'信息录入失败'];
            }
        }
        return view('home.application.index2');
    }

    /**
     * 完成申请页面3
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function index3(){
        if(!Session::get('application_userinfo') && !Session::get('role')){
            return redirect(url('application'));
        }
        return view('home.application.index3');
    }

    /**
     * 获取地址
     * @param $pid 上级地址id
     * @return mixed
     */
    public function getAddress($pid){
        $address=DB::table('address')->where('pid',$pid)->get();
        return $address;
    }
}
